﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace DataAccessor.Example
{
    public partial class FormAddEmployee : Form
    {
        public FormAddEmployee()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Open photo picker
        /// </summary>
        private void picPhoto_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog fileSelector = new OpenFileDialog();
            fileSelector.DefaultExt = "*.jpg";
            var result = fileSelector.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                this.picPhoto.ImageLocation = fileSelector.FileName;
            }
            fileSelector.Dispose();
        }


        /// <summary>
        /// Submit use config.
        /// </summary>
        private void btnUseConfig_Click(object sender, EventArgs e)
        {
            try
            {
                ExecuteFromConfig();
                this.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Create an exmplyee use the sql configruated in app_data/t-sql/Northwind/Employees.xml document,
        /// which appointed at the the holistic.xml document.
        /// Notice:The sequence of "params objects[] paramValueArray" must same with the statementName's value appoint statement's params.
        /// </summary>
        private void ExecuteFromConfig()
        {
            var photoStream = new FileStream(picPhoto.ImageLocation, FileMode.Open);
            byte[] photoBytes = new byte[photoStream.Length];

            for (long index = 0; index < photoStream.Length; index++)
            {
                photoBytes[index] = (byte)photoStream.ReadByte();
            }

            Program.DA.ExecuteNonQuery("Insert a employee",
                this.txtFirstName.Text,
                this.txtLastName.Text,
                this.txtTitle.Text,
                this.comboTitleOfCourtesy.Text,
                this.dpickerBirthDate.Value,
                this.dpickerHireDate.Value,
                this.txtAddress.Text,
                this.txtCity.Text,
                this.txtRegion.Text,
                this.txtPostalCode.Text,
                this.txtCountry.Text,
                this.txtHomePhone.Text,
                this.txtExtension.Text,
                photoBytes,
                this.richNotes.Text,
                this.comboReportsTo.Text,
                this.picPhoto.ImageLocation);
        }

        /// <summary>
        /// Submit use t-sql
        /// </summary>
        private void ExecuteFromSQL()
        {
            // Program.DA
        }
    }
}
